package com.sbl.bajw.pc.action;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.ExcelUtil;
import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.pc.model.PrizeAlarmModel;
import com.sbl.bajw.pc.service.PrizeAlarmModelService;
import com.sbl.bajw.util.LogUtil;

@Controller
@RequestMapping("/PrizeAlarm")
public class PrizeAlarmModelAction {

	@Resource
	private PrizeAlarmModelService service;
	
	static Logger log;
	
	static{
		log = LogUtil.getLogger("PrizeAlarm");
	}
	
	@RequestMapping(value = "/insert")
	public @ResponseBody String insert(HttpServletResponse response, HttpServletRequest request) throws MessageException{
		PrizeAlarmModel model = RequestUnit.getModel(PrizeAlarmModel.class);
		String json = "";
		try {
			log.info(model.getCreateBy() + "上传文件");
			service.insert(model);
			json = JsonResultUtil.Success();
		} catch (Exception e) {
			log.info(e.getMessage());
			e.printStackTrace();
			throw new MessageException("上传失败", "3");
		}
		return json;
	}
	
	@RequestMapping(value = "/getPrizeAlarmList")
	public @ResponseBody String getPrizeAlarmList(HttpServletResponse response, HttpServletRequest request){
		PrizeAlarmModel model = RequestUnit.getModel(PrizeAlarmModel.class);
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(model.getAddr() != null && !model.getAddr().equals("")){
			parm.put("addr", "%" + model.getAddr() + "%");
		}
		if(model.getContent() != null && !model.getContent().equals("")){
			parm.put("content", "%" + model.getContent() + "%");
		}
		if(model.getStart_date() != null && !model.getStart_date().equals("")){
			parm.put("start_date", model.getStart_date() + " 00:00:00");
		}
		if(model.getStop_date() != null && !model.getStop_date().equals("")){
			parm.put("stop_date", model.getStop_date() + " 23:59:59");
		}
		if(model.getUsername() != null && !model.getUsername().equals("")){
			parm.put("username", "%" + model.getUsername() + "%");
		}
		String json = service.getPrizeAlarmList(parm);
		return json;
	}
	
	@RequestMapping(value = "/downloadPrizeAlarmList")
	public void downloadPrizeAlarmList(HttpServletResponse response, HttpServletRequest request) throws Exception{
		PrizeAlarmModel model = RequestUnit.getModel(PrizeAlarmModel.class);
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(model.getAddr() != null && !model.getAddr().equals("")){
			parm.put("addr", "%" + model.getAddr() + "%");
		}
		if(model.getContent() != null && !model.getContent().equals("")){
			parm.put("content", "%" + model.getContent() + "%");
		}
		if(model.getStart_date() != null && !model.getStart_date().equals("")){
			parm.put("start_date", model.getStart_date() + " 00:00:00");
		}
		if(model.getStop_date() != null && !model.getStop_date().equals("")){
			parm.put("stop_date", model.getStop_date() + " 23:59:59");
		}
		if(model.getUsername() != null && !model.getUsername().equals("")){
			parm.put("username", "%" + model.getUsername() + "%");
		}
		List<HashMap<String, Object>> list = service.downloadPrizeAlarmList(parm);
		
		HashMap<String, String> column = new HashMap<String, String>();
		column.put("ID", "编号");
		column.put("ADDR", "报警地址");
		column.put("CONTENT", "报警内容");
		column.put("MAPX", "报警坐标经度");
		column.put("MAPY", "报警坐标纬度");
		column.put("SHOWNAME", "报警人");
		column.put("CREATE_TIME", "报警时间");
		
		HSSFWorkbook wb = ExcelUtil.createExcel(list, column);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=prize_alarm.xls");  
		wb.write(outStream);
	}
	
	@RequestMapping(value = "/getPrizeAlarmDetails")
	public ModelAndView getPrizeAlarmDetails(HttpServletResponse response, HttpServletRequest request){
		String id = RequestUnit.getString("id");
		HashMap<String, Object> model = service.getModel(id);
		List<HashMap<String, Object>> list = service.getAttfileByPrizeAlarmId(id);
		request.setAttribute("model", model);
		request.setAttribute("list", list);
		ModelAndView mav = new ModelAndView("/view/manager/prizeAlarm/details");
		return mav;
	}
}
